/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cartones;

import forms.Juego;
import forms.Ventana;
import java.sql.ResultSet;
import megabingo.MegaBingo;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import system.Exc;
import system.Persistencia;

/**
 *
 * @author gilberto
 */
public class Main {

    private static String sql = "", tabla_schema;
    private static int inicio, fin;
    private static Find find = new Find();
    private static List<Beans> listBeans = new ArrayList<>();
    private static Map<String, Integer> map = new HashMap<>();
    private static Persistencia persistencia = new Persistencia();

    public static void Main() throws Exc, SQLException, Exception {
        tabla_schema = MegaBingo.schema + ".balota" + MegaBingo.balota;
        map = find.inicio_fin(MegaBingo.modoJuego, tabla_schema);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getKey().equals("inicio")) {
                inicio = Integer.parseInt(entry.getValue().toString());
            } else {
                fin = Integer.parseInt(entry.getValue().toString());
            }
        }
        Jugar();
        Cantar_Ganador();
    }

    public static void Jugar() {
        try {
            int id_carton, column;
            ResultSet rsts = null;
            for (int i = 1; i < 76; i++) {
                MegaBingo.faltan[i] = 0;
            }
            sql = "SELECT detalle FROM " + tabla_schema + " WHERE id BETWEEN " + inicio + " AND " + fin;
            rsts = persistencia.executeRsts(sql);
            while (rsts.next()) {
                id_carton = rsts.getInt("detalle");
                if (MegaBingo.modoJuego == 0) {
                    MegaBingo.arreglo[id_carton][0] = MegaBingo.arreglo[id_carton][0] - 1;
                } else {
                    column = MegaBingo.FILA;
                    if (Columna(MegaBingo.balota) == column) {
                        MegaBingo.arreglo[id_carton][column] = MegaBingo.arreglo[id_carton][column] - (100 + MegaBingo.balota);
                        if (MegaBingo.arreglo[id_carton][column] > 0 && MegaBingo.arreglo[id_carton][column] < 76) {
                            MegaBingo.faltan[MegaBingo.arreglo[id_carton][column]]++;
                        }
                    }
                }
            }

        } catch (Exc ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Inicializar_Array() {
        for (int i = 1; i < 500001; i++) {
            for (int j = 1; j < 6; j++) {
                MegaBingo.arreglo[i][j] = 0;
            }
        }
    }

    public static void Cargar_Array() {
        try {
            if (MegaBingo.modoJuego == 0) {
                sql = "SELECT id_carton, suma, fila AS detalle FROM public." + MegaBingo.tabla;
                listBeans = find.listarTodos(sql);
                Iterator it = listBeans.iterator();
                while (it.hasNext()) {
                    Beans obj = (Beans) it.next();
                    MegaBingo.arreglo[obj.getId_carton()][0] = 25;
                }
                listBeans.clear();
            } else {
                sql = "SELECT id_carton, suma, fila AS detalle FROM public." + MegaBingo.tabla;
                listBeans = find.listarTodos(sql);
                Iterator it = listBeans.iterator();
                while (it.hasNext()) {
                    Beans obj = (Beans) it.next();
                    MegaBingo.arreglo[obj.getId_carton()][obj.getDetalle()] = obj.getSuma();
                }
                listBeans.clear();
            }

        } catch (Exc ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "WARNING_MESSAGE", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "WARNING_MESSAGE", JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getLocalizedMessage(), "WARNING_MESSAGE", JOptionPane.ERROR_MESSAGE);
        }
    }

    private static void Cantar_Ganador() throws Exc, SQLException, Exception {
        int k = 0;
        int column = MegaBingo.FILA;
        if (MegaBingo.modoJuego == 0) {
            persistencia.dbConnect();
            for (int i = 1; i < 500001; i++) {
                if (MegaBingo.arreglo[i][0] == 0) {
                    Ventana.jTable1.setValueAt(i, k, 0);
                    Insertar_Ganadores(i);
                    k++;
                    MegaBingo.ganadores++;
                }
            }
            persistencia.dbDisconnect();
        } else {
            persistencia.dbConnect();
            for (int i = 1; i < 500001; i++) {
                if (MegaBingo.arreglo[i][column] == -100) {
                    Ventana.jTable1.setValueAt(i, k, 0);
                    Insertar_Ganadores(i);
                    k++;
                    MegaBingo.ganadores++;
                }
            }
            persistencia.dbDisconnect();
        }
        if (MegaBingo.ganadores > 0) {
            JOptionPane.showMessageDialog(null, "Hay " + MegaBingo.ganadores + " ganadores", "WARNING_MESSAGE", JOptionPane.ERROR_MESSAGE);
        }
    }

    private static void Insertar_Ganadores(int id_carton) {
        try {
            sql = "INSERT INTO cartones_ganadores (id_carton, numero_bingo, detalle_bingo) VALUES ("
                    + id_carton + ", " + Juego.numero_bingo.getText() + ", '" + Juego.detalle_bingo.getText() + "')";
            persistencia.executeUpdate(sql);
        } catch (Exc ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static int Columna(int numero) {
        int column = 0;
        if (numero <= 15) {
            column = 1;
        } else if (numero > 15 && numero <= 30) {
            column = 2;
        } else if (numero > 30 && numero <= 45) {
            column = 3;
        } else if (numero > 45 && numero <= 60) {
            column = 4;
        } else {
            column = 5;
        }
        return column;
    }
}
